package com.example.votesystem.controller;


import com.example.votesystem.constant.MessageConstant;
import com.example.votesystem.entity.Result;
import com.example.votesystem.pojo.TGroup;
import com.example.votesystem.service.CheckGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 检查组管理
 */

@RestController
@RequestMapping("/checkgroup")
public class CheckGroupController {
    @Autowired
    private CheckGroupService checkGroupService;
    //新增检查组
    @PostMapping("/add")
    public Result add(@RequestParam(value = "Group") String group){
        try{
            checkGroupService.add(group);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_CHECKGROUP_FAIL);
        }
        return new Result(true,MessageConstant.ADD_CHECKGROUP_SUCCESS);
    }

    //修改检查组
    @PostMapping("/update")
    public Result update(@RequestParam(value = "id") Integer id,@RequestParam(value = "Group") String group){
        try{
            checkGroupService.update(id,group);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_CHECKGROUP_FAIL);
        }
        return new Result(true,MessageConstant.EDIT_CHECKGROUP_SUCCESS);
    }

    //删除检查组
    @PostMapping("/delete")
    public Result delete(@RequestParam(value = "id") Integer id){
        try{
            checkGroupService.delete(id);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_CHECKGROUP_FAIL);
        }
        return new Result(true,MessageConstant.DELETE_CHECKGROUP_SUCCESS);
    }

    //查询所有检查组
    @PostMapping("/findAll")
    public Result findAll(){
        try{
            List<TGroup> list = checkGroupService.findAll();
            return new Result(true,MessageConstant.QUERY_CHECKGROUP_SUCCESS,list);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_CHECKGROUP_FAIL);
        }
    }
}
